﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NCHQMES
{
    public partial class FrmErr : Form
    {
        public FrmErr()
        {
            InitializeComponent();
            this.Location = PointToClient(Cursor.Position);
            List<ErrClass> errClasses = Method.JsonToList<ErrClass>(Method.ReadPath<ErrClass>());
            if (errClasses != null)
            {
                dataGridView1.DataSource = new BindingSource(new BindingList<ErrClass>(errClasses), null);
            }
            panel1.Enabled = false;
        }

        private void btnRecompose_Click(object sender, EventArgs e)
        {
            panel1.Enabled = true;
            btnRecompose.BackColor = Color.Red;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            List<ErrClass> errClasses = new List<ErrClass>();
            try
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    ErrClass errClass = new ErrClass
                    {
                        ErrMsg = row.Cells["ErrMsg"].Value == null ? "" : row.Cells["ErrMsg"].Value.ToString(),
                        Code = row.Cells["Code"].Value == null ? "" : row.Cells["Code"].Value.ToString(),
                        MesCode = row.Cells["MesCode"].Value == null ? "" : row.Cells["MesCode"].Value.ToString()
                    };
                    errClasses.Add(errClass);
                }

                if (Method.WritePathTxt<ErrClass>(Method.ListToJson(errClasses)))
                {
                    MessageBox.Show("保存成功");
                    panel1.Enabled = false;
                    btnRecompose.BackColor = Color.WhiteSmoke;
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误信息" + ex.Message.ToString());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}